﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuDada.Core.Post.Domain;
using Common.Logging;
using WuDada.Core.Post;
using WuDada.Core.Post.Service;
using System.Text;

public partial class act_all : System.Web.UI.Page
{
    private ILog m_Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private PostFactory m_PostFactory;
    private IPostService m_PostService;

    //近期活動NodeId=6
    private int m_TopNodeId = 6;
    private int m_NodeId = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        m_PostFactory = new PostFactory();
        m_PostService = m_PostFactory.GetPostService();

        if (!IsPostBack)
        {
            m_NodeId = QueryStringHelper.GetInteger("nodeId", 0);

            //清單
            Repeater_Bind();

            if (m_NodeId != 0)
            {
                LoadDataToUI();
            }
        }
    }

    private void LoadDataToUI()
    {
        NodeVO nodeVO = m_PostService.GetNodeById(m_NodeId);
        ltlNodeName.Text = nodeVO.Name;
    }

    private void Repeater_Bind()
    {
        DateTime? startDate = DateTime.Today;

        string nodeQuery = string.Empty;
        if (m_NodeId != 0)
        {
            nodeQuery = string.Format("AND Node.NodeId = {0} ", m_NodeId);
        }

        //搜尋條件
        StringBuilder sbHql = new StringBuilder();
        sbHql.Append(string.Format(@"WHERE Flag = {0} 
AND Node.ParentNode.NodeId = {1} AND CloseDate >= '{2}' 
{3} "
           , 1, m_TopNodeId, startDate.Value.ToShortDateString(), nodeQuery));

        //分頁
        AspNetPager1.RecordCount = m_PostService.CountPostByWhere(sbHql.ToString());
        int pageIndex = (AspNetPager1.CurrentPageIndex - 1);
        int pageSize = AspNetPager1.PageSize;
        sbHql.Append(" ORDER BY ShowDate Desc ");

        Repeater1.DataSource = m_PostService.SearchPostByWhere(sbHql.ToString(), pageIndex, pageSize);
        Repeater1.DataBind();
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        Repeater_Bind();
    }

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            Control ctrl = e.Item;
            PostVO postVO = (PostVO)e.Item.DataItem;

            //圖片
            if (!string.IsNullOrEmpty(postVO.PicFileName))
            {
                string img = string.Format("<img src='upload/{0}' width='70' height='70' class='news-pp' />"
                    , postVO.PicFileName);
                UIHelper.SetLiteralText(ctrl, "ltlImg", img);
            }

            //日期
            string date = postVO.ShowDate.Value.ToString("yyyy.MM.dd");
            UIHelper.SetLiteralText(ctrl, "ltlDate"
                , date);

            //標題
            UIHelper.SetLiteralText(ctrl, "ltlTitle", postVO.Title);
        }
    }
}